package com.lee.bgTask.thread;

import com.lee.bgTask.entity.impl.AbstractTaskEntity;
import com.lee.bgTask.queue.TaskQueue;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class BgTaskThread extends Thread {

    private TaskQueue taskQueue;

    AbstractTaskEntity taskEntity;

    public BgTaskThread(TaskQueue taskQueue) {
        super();
        this.taskQueue = taskQueue;
    }

    @Override
    public void run() {
        while (true) {
            taskEntity = taskQueue.get();
            if (taskEntity == null) {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                    break;
                }

            } else {
                taskEntity.onExcute();
                try {
                    taskQueue.submit(taskEntity);
                } catch (Exception ex) {
                    System.out.println(taskEntity + " 执行失败");
                    ex.printStackTrace();
                }
            }
        }
    }

}
